package org.jsshbroadcast.ui.terminal;

/**
 *
 * @author Eduardo Folly
 */
public class MascaraSenha extends Thread {

    private volatile boolean stop;
    private char echochar = '*';

    public MascaraSenha(String prompt) {
        System.out.print(prompt);
    }

    @Override
    @SuppressWarnings("static-access")
    public void run() {

        int priority = Thread.currentThread().getPriority();
        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

        try {
            stop = true;
            while (stop) {
                System.out.print("\010" + echochar);
                try {
                    // attempt masking at this rate
                    Thread.currentThread().sleep(1);
                } catch (InterruptedException iex) {
                    Thread.currentThread().interrupt();
                    return;
                }
            }
        } finally { // restore the original priority
            Thread.currentThread().setPriority(priority);
        }
    }

    public void stopMasking() {
        this.stop = false;
    }
}